package com.chuangke.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chuangke.admin.entity.SysUser;
import com.chuangke.admin.service.SysUserService;
import com.chuangke.admin.util.UserUtil;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.idgen.IdManager;
import com.chuangke.system.dao.mapper.SysCommentMapper;
import com.chuangke.system.entity.SysComment;
import com.chuangke.system.service.SysCommentService;

@Service
public class SysCommentServiceImpl implements SysCommentService {

	@Autowired
	private SysCommentMapper sysCommentMapper;
	@Autowired
	private SysUserService sysUserService;

	@Override
	public void save(SysComment sysComment) {
		sysComment.setId(IdManager.nextId());
		sysCommentMapper.insert(sysComment);
	}

	@Override
	public List<SysComment> findByTopic(CkPageQuery page) {
		QueryWrapper<SysComment> queryWrapper= new QueryWrapper<>();
		String topicType=(String)page.get("topicType");
		if(topicType!=null&& !topicType.isEmpty()) {
			queryWrapper.lambda().eq(SysComment::getTopicType, topicType);
		}
		String topicId=(String)page.get("topicId");
		if(topicId!=null&& !topicId.isEmpty()) {
			queryWrapper.lambda().eq(SysComment::getTopicId, topicId);
		}
		String commentBy=(String)page.get("commentBy");
		if(commentBy!=null&& !commentBy.isEmpty()) {
			queryWrapper.lambda().eq(SysComment::getCommentBy, commentBy);
		}
		queryWrapper.lambda().orderByDesc(SysComment::getCommentAt);
		List<SysComment> list = sysCommentMapper.selectList(queryWrapper);
		loadUser(list);
		loadTopic(list) ;
		return list;
	}
	
	private void loadUser(List<SysComment> list) {
		if (list == null || list.isEmpty()) {
			return ;
		}

		Map<String, SysUser> userMap = sysUserService.findMap();
		list.forEach(comment -> {
			SysUser user = userMap.get(comment.getCommentBy());
			comment.setCommentByName(user == null ? "" : user.getNickName());
			comment.setCommentByUser(UserUtil.getSimpleSysUser(user));
		});
	}
	
	private void loadTopic(List<SysComment> list) {
		if (list == null || list.isEmpty()) {
			return;
		}

		list.forEach(sysComment -> {
			if (SysComment.TOPIC_TYPE_TASK.equals(sysComment.getTopicType())) {
//				ReponseTask reponseTask = taskQueryService.findById(sysComment.getTopicId());
//				sysComment.setTopicName(reponseTask == null ? sysComment.getTopicId() + "" : reponseTask.getName());
			}
		});
	}

	@Override
	public List<SysComment> getListByTopId(String id,String type) {
		return sysCommentMapper.selectList(new QueryWrapper<SysComment>().lambda().eq(SysComment::getTopicId,id).eq(SysComment::getTopicType,type).orderByDesc(SysComment::getCommentAt));
	}

	@Override
	public void update(String objType, String objId, String objName) {
		sysCommentMapper.update(null, new UpdateWrapper<SysComment>().lambda().set(SysComment::getTopicName, objName)
				.eq(SysComment::getTopicType, objType).eq(SysComment::getTopicId, objId));
	}

}
